1 package edu.jiangxin.apktoolbox.help.settings; 2 3 import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel; 4 import edu.jiangxin.apktoolbox.utils.Constants; 5 import org.apache.commons.collections4.CollectionUtils; 6 import org.apache.commons.io.FileUtils; 7 8 import javax.swing.*; 9 import java.io.File; 10 import java.io.IOException; 11 import java.net.URL; 12 import java.nio.file.Files; 13 import java.util.Collection; 14 15 public class AddToStartupPanel extends EasyChildTabbedPanel { 16 17 private static final String STARTUP_FILE; 18 19 private JPanel optionPanel; 20 21 static { 22 final String SEPARATOR = File.separator; 23 final String STARTUP_FOLDER; 24 if (System.getProperty("os.name").toLowerCase().contains("win")) { 25 STARTUP_FOLDER = System.getenv("APPDATA") + SEPARATOR + "Microsoft" + SEPARATOR + "Windows" + SEPARATOR + "Start Menu" + SEPARATOR + "Programs" + SEPARATOR + "Startup"; 26 } else if (System.getProperty("os.name").toLowerCase().contains("mac")) { 27 STARTUP_FOLDER = System.getProperty("user.home") + SEPARATOR + "Library" + SEPARATOR + "LaunchAgents"; 28 } else { 29 STARTUP_FOLDER = System.getProperty("user.home") + SEPARATOR + ".config" + SEPARATOR + "autostart"; 30 } 31 STARTUP_FILE = STARTUP_FOLDER + SEPARATOR + "ApkToolBoxGUI.lnk"; 32 } 33 34 @Override 35 public void createUI() { 36 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS); 37 setLayout(boxLayout); 38 39 createOptionPanel(); 40 add(optionPanel); 41 42 add(Box.createVerticalStrut(15 * Constants.DEFAULT_Y_BORDER)); 43 } 44 45 private void createOptionPanel() { 46 optionPanel = new JPanel(); 47 optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS)); 48 49 JLabel typeLabel = new JLabel("Add to startup:"); 50 JCheckBox addToStartupCheckBox = new JCheckBox(); 51 addToStartupCheckBox.setSelected(conf.getBoolean("add.to.startup", false)); 52 addToStartupCheckBox.addActionListener(e -> { 53 conf.setProperty("add.to.startup", addToStartupCheckBox.isSelected()); 54 if (addToStartupCheckBox.isSelected()) { 55 addToStartup(); 56 } else { 57 FileUtils.deleteQuietly(new File(STARTUP_FILE)); 58 } 59 }); 60 61 optionPanel.add(typeLabel); 62 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 63 optionPanel.add(addToStartupCheckBox); 64 } 65 66 private void addToStartup() { 67 String executableFilePath = getExecutableFilePath(); 68 if (executableFilePath == null) { 69 return; 70 } 71 72 File shortcutFile = new File(STARTUP_FILE); 73 74 if (!shortcutFile.exists()) { 75 try { 76 createShortcut(executableFilePath); 77 logger.info("create shortcut file successfully: {}", STARTUP_FILE); 78 } catch (IOException e) { 79 logger.error("create shortcut file failed: {}", STARTUP_FILE, e); 80 } 81 } 82 } 83 84 private String getExecutableFilePath() { 85 URL jarUrl = AddToStartupPanel.class.getProtectionDomain().getCodeSource().getLocation(); 86 if (jarUrl == null) { 87 logger.error("jarUrl is null"); 88 return null; 89 } 90 File jarFile = new File(jarUrl.getPath()); 91 logger.info("jarFile: {}", jarFile.getAbsolutePath()); 92 File grandpaFile = jarFile.getParentFile().getParentFile(); 93 logger.info("parentFile: {}", grandpaFile.getAbsolutePath()); 94 Collection<File> executableFiles = FileUtils.listFiles(grandpaFile, new String[]{"exe"}, false); 95 if (CollectionUtils.isEmpty(executableFiles)) { 96 logger.error("Can not find executable file"); 97 return null; 98 } 99 if (executableFiles.size() > 1) { 100 logger.error("There are more than one executable file"); 101 return null; 102 } 103 File executableFile = executableFiles.iterator().next(); 104 logger.info("executableFile: {}", executableFile.getAbsolutePath()); 105 return executableFile.getAbsolutePath(); 106 } 107 108 private static void createShortcut(String targetPath) throws IOException { 109 String vbsScript = """ 110 Set WshShell = CreateObject("WScript.Shell") 111 Set Shortcut = WshShell.CreateShortcut("%s") 112 Shortcut.TargetPath = "%s" 113 Shortcut.Save 114 """.formatted(STARTUP_FILE, targetPath); 115 116 File tempVbs = Files.createTempFile("createShortcut", ".vbs").toFile(); 117 Files.writeString(tempVbs.toPath(), vbsScript); 118 119 Runtime.getRuntime().exec("wscript " + tempVbs.getAbsolutePath()); 120 tempVbs.deleteOnExit(); 121 } 122 }